package com.zp.business.impl.outpFees;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zp.business.entity.outpFees.OutpFeesPaymentsMoney;
import com.zp.business.entity.outpFees.bo.OutpFeesPaymentsMoneyBo;
import com.zp.business.entity.outpFees.vo.OutpFeesPaymentsMoneyVo;
import com.zp.business.mapper.outpFees.OutpFeesPaymentsMoneyMapper;
import com.zp.business.service.outpFees.IOutpFeesPaymentsMoneyService;
import com.zp.common.core.constant.CacheNames;
import com.zp.common.core.utils.StringUtils;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import com.zp.common.redis.utils.CacheUtils;
import com.zp.system.api.entity.SysDictData;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 门诊收费支付类型方式Service业务层处理
 *
 * @author zhang peng
 * @ date 2024-04-12
 */
@RequiredArgsConstructor
@Service
public class OutpFeesPaymentsMoneyServiceImpl implements IOutpFeesPaymentsMoneyService {

    private final OutpFeesPaymentsMoneyMapper baseMapper;

    /**
     * 支付类型
     */
    static List<SysDictData> payWayDictMap = CacheUtils.get(CacheNames.SYS_DICT, CacheNames.PAY_WAY_DICT);

    /**
     * 查询门诊收费支付类型方式
     */
    @Override
    public OutpFeesPaymentsMoneyVo queryById (String outpFeesPaymentsId) {
        return baseMapper.selectVoById(outpFeesPaymentsId);
    }

    /**
     * 查询门诊收费支付类型方式列表(分页)
     */
    @Override
    public TableDataInfo<OutpFeesPaymentsMoneyVo> queryPageList (OutpFeesPaymentsMoneyBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<OutpFeesPaymentsMoney> lqw = buildQueryWrapper(bo);
        Page<OutpFeesPaymentsMoneyVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询门诊收费支付类型方式列表(不分页)
     */
    @Override
    public List<OutpFeesPaymentsMoneyVo> queryList (OutpFeesPaymentsMoneyBo bo) {
        LambdaQueryWrapper<OutpFeesPaymentsMoney> lqw = buildQueryWrapper(bo);
        List<OutpFeesPaymentsMoneyVo> outpFeesPaymentsMoneyVos = baseMapper.selectVoList(lqw);
        outpFeesPaymentsMoneyVos.forEach(outpFeesPaymentsMoneyVo -> {
            outpFeesPaymentsMoneyVo.setMoneyTypeName(CollectionUtil.isNotEmpty(payWayDictMap) && StringUtils.isNotBlank(outpFeesPaymentsMoneyVo.getMoneyType()) ?
                    payWayDictMap.stream()
                            .filter(i -> StringUtils.equals(outpFeesPaymentsMoneyVo.getMoneyType(), i.getDictValue()))
                            .findAny().get().getDictLabel() : "");
        });
        return outpFeesPaymentsMoneyVos;
    }

    private LambdaQueryWrapper<OutpFeesPaymentsMoney> buildQueryWrapper (OutpFeesPaymentsMoneyBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<OutpFeesPaymentsMoney> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getOutpFeesPaymentsId()), OutpFeesPaymentsMoney::getOutpFeesPaymentsId, bo.getOutpFeesPaymentsId());
        lqw.eq(StringUtils.isNotBlank(bo.getOutpFeesMasterId()), OutpFeesPaymentsMoney::getOutpFeesMasterId, bo.getOutpFeesMasterId());
        lqw.eq(StringUtils.isNotBlank(bo.getFeesNo()), OutpFeesPaymentsMoney::getFeesNo, bo.getFeesNo());
        lqw.eq(StringUtils.isNotBlank(bo.getSerialNo()), OutpFeesPaymentsMoney::getSerialNo, bo.getSerialNo());
        lqw.eq(StringUtils.isNotBlank(bo.getMoneyType()), OutpFeesPaymentsMoney::getMoneyType, bo.getMoneyType());
        lqw.eq(bo.getCharges() != null, OutpFeesPaymentsMoney::getCharges, bo.getCharges());
        lqw.eq(StringUtils.isNotBlank(bo.getOrgId()), OutpFeesPaymentsMoney::getOrgId, bo.getOrgId());
        lqw.eq(StringUtils.isNotBlank(bo.getRemarks()), OutpFeesPaymentsMoney::getRemarks, bo.getRemarks());
        return lqw;
    }

    /**
     * 新增门诊收费支付类型方式
     */
    @Override
    public Boolean insertByBo (OutpFeesPaymentsMoneyBo bo) {
        OutpFeesPaymentsMoney add = BeanUtil.toBean(bo, OutpFeesPaymentsMoney.class);
        validEntityBeforeSave(add);
        add.initEntity();
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setOutpFeesPaymentsId(add.getOutpFeesPaymentsId());
        }
        return flag;
    }

    /**
     * 修改门诊收费支付类型方式
     */
    @Override
    public Boolean updateByBo (OutpFeesPaymentsMoneyBo bo) {
        OutpFeesPaymentsMoney update = BeanUtil.toBean(bo, OutpFeesPaymentsMoney.class);
        update.updateInfo();
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave (OutpFeesPaymentsMoney entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除门诊收费支付类型方式
     */
    @Override
    public Boolean deleteWithValidByIds (Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
